package org.example.controller;

import org.example.common.Result;
import org.example.common.ResultCode;
import org.example.entity.Admin;
import org.example.service.AdminService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

  @Resource
  AdminService adminService;

  @CrossOrigin
  @GetMapping("/alldata")
  public Result getData(){
  //获取数据
  List<Admin> admins = adminService.GetAll();
  //admin表中的数据如果少于5 抛出异常
  if (admins.size()<5){
    //抛出异常 给用户 前端
    return  Result.error(ResultCode.DATA_LESS.msg);
//        throw new CustomException(ResultCode.DATA_LESS);
  }
  //表示请求成功
  return Result.success(admins);
  }


  /**
   * 描述：根据ID删除
   */
  @CrossOrigin
  @DeleteMapping("/{id}")
  public Result delete(@PathVariable Integer id) {
    adminService.delete(id);
    return Result.success();
  }

  /**
   * 编辑管理员信息接口
   */
  @PostMapping("/edit")
  public Result adminEdit(@RequestBody Admin admin){
    return Result.success(adminService.adminEdit(admin));

  }

  /**
   * 描述：分页查询
   */
  @PostMapping("/page")
  public Result page(@RequestBody Admin search,
                     @RequestParam(defaultValue = "1") Integer pageNum,
                     @RequestParam(defaultValue = "5") Integer pageSize) {
    return Result.success(adminService.findPage(search, pageNum, pageSize));
  }

}
